﻿using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Base;
using NAGFwk.BL;
using NAGFwk.UI;
using SilkeyERP.BL;

namespace SilkeyERP.UI.Entities.BankAccounts
{
    public partial class ContentView : BaseContentDetail
    {
        private IBaseController<Model.BankAccounts> _controller = new BankAccountsController();

        public override string ModuleName { get { return "BankAccounts"; } }

        public ContentView()
        {
            InitializeComponent();
        }

        public override void ShowModule(bool firstShow)
        {
            base.ShowModule(firstShow);
            MapItLoad(_controller);
            bankBindingSource.DataSource = new BankController().GetAll();
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            MapItDelete(_controller);
        }

        protected override void ValidateRows(ValidateRowEventArgs e)
        {
            var typedRow = (Model.BankAccounts)e.Row;
            if(string.IsNullOrEmpty(typedRow.AccountNumber))
            {
                e.ErrorText = "Numero de Cuenta es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.OwnerEmail))
            {
                e.ErrorText = "Correo electronico del Propietario es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.OwnerIdentificationNumber.ToString()))
            {
                e.ErrorText = "DNI Propietario es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.OwnerName))
            {
                e.ErrorText = "Nombre Propietario es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.OwnerPhone.ToString()))
            {
                e.ErrorText = "Telefono Propietario es Requerido.";
                e.Valid = false;
            }
            if (typedRow.BankID == int.MinValue)
            {
                e.ErrorText = "Banco es Requerido.";
                e.Valid = false;
            }
        }

        private void RefreshAll()
        {
            _controller = MapItRefresh(new BankAccountsController());
            bankBindingSource.DataSource = new BankController().GetAll();
        }

        private void bbiRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshAll();
        }
    }
}
